#!/usr/bin/tclsh

#
# Copy source and uuid files from all .inform projects
# in the given project directory to the given source
# control directory.
#
# But, only if those files exist in both places and
# the Inform project file was modified after the 
# file under source control.
#
set now [clock format [clock seconds]]
if { $argc != 2 } {
    puts "Usage: $argv0 <projects directory> <repo directory>"
    exit 1
}
set projectsDir [lindex $argv 0]
set scmDir [lindex $argv 1]

cd $projectsDir
foreach proj [glob *.inform] {
    if ![file exists [file join $scmDir $proj Source story.ni]] {
    	continue
    }
    if ![file exists [file join $scmDir $proj uuid.txt]] {
    	continue
    }
    if ![file exists [file join $proj Source story.ni]] {
    	continue
    }
    if ![file exists [file join $proj uuid.txt]] {
    	continue
    }
    set uuid [file join $proj uuid.txt]
    set uuidScm [file join $scmDir $uuid]
    set ni [file join $proj Source story.ni]
    set niScm [file join $scmDir $ni]
    set uuidMtime [file mtime $uuid]
    set niMtime [file mtime $ni]
    set uuidScmMtime [file mtime $uuidScm]
    set niScmMtime [file mtime $niScm]
    if [expr $uuidMtime - $uuidScmMtime] {
    	file copy -force $uuid $uuidScm
	puts "$now: copied $uuid to $uuidScm"
    }
    if [expr $niMtime - $niScmMtime] {
    	file copy -force $ni $niScm
	puts "$now: copied $ni to $niScm"
    }
}

